package org.linlinjava.litemall.admin.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.linlinjava.litemall.admin.beans.vo.TaxVo;
import org.linlinjava.litemall.admin.dto.LitemallTaxDTO;
import org.linlinjava.litemall.admin.util.AdminResponseEnum;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallRegion;
import org.linlinjava.litemall.db.domain.LitemallTax;
import org.linlinjava.litemall.db.service.LitemallRegionService;
import org.linlinjava.litemall.db.service.LitemallTaxService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 管理税务服务
 *
 * @author yliyun
 * @description：TODO
 * @date 2022/09/14
 */
@Service
public class AdminTaxService {

    /**
     * litemall税务服务
     */
    @Autowired
    private LitemallTaxService litemallTaxService;
    /**
     * litemall地区服务
     */
    @Autowired
    private LitemallRegionService litemallRegionService;

    /**
     * 创建
     *
     * @param tax 税
     * @return {@link Object}
     */
    public Object create(LitemallTax tax){
        if(litemallTaxService.exist(tax)){
            return ResponseUtil.fail(AdminResponseEnum.TAX_EXIST);
        }
        litemallTaxService.add(tax);
        return ResponseUtil.ok();
    }

    /**
     * 更新
     *
     * @param tax 税
     * @return {@link Object}
     */
    public Object update(LitemallTaxDTO tax){
        if(ObjectUtil.isEmpty(litemallTaxService.findById(tax.getId()))){
            return ResponseUtil.fail(AdminResponseEnum.TAX_NOT_EXIST);
        }
        return litemallTaxService.update(tax)>0?ResponseUtil.ok():ResponseUtil.fail();
    }

    /**
     * 查询选择性
     *
     * @param regionId 区域id
     * @param code     代码
     * @param page     页面
     * @param size     大小
     * @param sort     排序
     * @param order    订单
     * @return {@link Object}
     */
    public Object querySelective(Integer regionId, String code, Integer page, Integer size, String sort, String order){
        IPage<LitemallTax> litemallTaxes = litemallTaxService.querySelective(regionId, code, page, size, sort, order);
        List<TaxVo> collect = litemallTaxes.getRecords().stream().map(tax -> {
            LitemallRegion region = litemallRegionService.findById(tax.getRegionId());
            TaxVo vo = new TaxVo();
            BeanUtils.copyProperties(tax, vo);
            if (region != null) {
                vo.setRegionNameCn(region.getNameCn());
                vo.setRegionNameEn(region.getNameEn());
            }
            return vo;
        }).collect(Collectors.toList());
        return ResponseUtil.okPage(litemallTaxes,collect);
    }


    /**
     * 删除
     *
     * @param id id
     * @return {@link Object}
     */
    public Object delete(Integer id){
        litemallTaxService.deleteById(id);
        return ResponseUtil.ok();
    }

    /**
     * 启用
     *
     * @param tax 税
     * @return {@link Object}
     */
    public Object enable(LitemallTax tax) {
        LitemallTax update = new LitemallTax();
        update.setId(tax.getId());
        update.setEnable(tax.getEnable());
        litemallTaxService.update(update);
        return ResponseUtil.ok();
    }

    /**
     * 按地区找到id
     *
     * @param firstRegionId  第一个区域id
     * @param secondRegionId 第二个区域id
     * @param regionId       区域id
     * @return {@link List}<{@link LitemallTax}>
     */
    public List<LitemallTax> findByRegionId(Integer firstRegionId, Integer secondRegionId, Integer regionId){
    	List<Integer> regionIdLs = new ArrayList<>();
    	regionIdLs.add(firstRegionId);
    	regionIdLs.add(secondRegionId);
    	regionIdLs.add(regionId);

        return litemallTaxService.queryEnabledByRegionIds(regionIdLs);
    }
}
